// 本文件对CPacket的相关功能进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/16 13:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const benchmark = require('benchmark')
const MINPacket = require('./MINPacket');
const {createPayLoad} = require("../common/createPayload");
const {createBlockByBuffer} = require("../encoding/Block");
const {CPacket} = require("./CPacket");
const {Encoder, MaxPacketSize} = require('../encoding').Encoder
const {createIdentifierByString} = require("../component/Identifier");
const payload = createPayLoad(0), payload1K = createPayLoad(1), payload8K = createPayLoad(8)

function createCPacket(payload) {
    let packet = new MINPacket()
    let wrapper = createIdentifierByString('/min/pku/sz')
    let wrapper2 = createIdentifierByString('/install')
    packet.packetType = 3
    let cPacket = new CPacket()
    cPacket.setSrcIdentifier(wrapper)
    cPacket.setDstIdentifier(wrapper2)
    cPacket.TTL.setTTL(16546418374324163n)
    cPacket.Payload.setValue(payload)
    return cPacket
}

let cPacket = createCPacket(payload), cPacket1K = createCPacket(payload1K), cPacket8K = createCPacket(payload8K)
const suite2 = new benchmark.Suite()
suite2.add('wireEncode 1Byte', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    cPacket.wireEncode(enc)
}).add('wireEncode 1K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    cPacket1K.wireEncode(enc)
}).add('wireEncode 8K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    cPacket8K.wireEncode(enc)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireEncode finished')
}).run()

let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
cPacket.wireEncode(enc)
let buf = enc.getBuffer()
let cPacket2 = new CPacket()
let block = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
cPacket1K.wireEncode(enc)
let buf1K = enc.getBuffer()
let cPacket2_1k = new CPacket()
let block1K = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
cPacket8K.wireEncode(enc)
let buf8K = enc.getBuffer()
let cPacket2_8K = new CPacket()
let block8K = createBlockByBuffer(buf, false)
const suite3 = new benchmark.Suite()
suite3.add('wireDecode 1Byte', ()=>{
    cPacket2.wireDecode(block)
}).add('wireDecode 1K', ()=>{
    cPacket2_1k.wireDecode(block1K)
}).add('wireDecode 8K', ()=>{
    cPacket2_8K.wireDecode(block8K)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireDecode finished')
}).run()
